#include "CS2103T_CE1.h"

#include "Data.h"
#include "FileInterface.h"
#include "Parser.h"

using namespace std;

/*
	CS 2103T CE1
	TextBuddy

	Author:
		Low WeiLin A0096566M

	Description:
		A simple program to manipulate text in a file.

	On when records/text are saved:
		The records are saved to file after every operation, 
		as what a user would expect in the event they accidently
		close the program or the program crashes unexpectedly.
		However, this program is written in a modular way such
		that it can be easily changed to save to file at other times.
		It would be difficult to save it at regular intervals
		(every 5 seconds) however, as another thread would be 
		required in this console application.


	Program Structure:
		This program is divided in to the following classes:
			Data - defines what content to hold and store in file. (a single text field in this program)
			FileInterface - holds a list of records(Data) and interfaces with the file system to manipulate the text file accordingly
			Parser - takes in and interprets user input, routing to the correct action and providing output, error messages.

		Each class is defined in their .h file and implemented in the respective .cpp file.

		The main function(entry point) initializes everything and handles getting input from the user and passing it to the parser.
*/


int main(int argc, char *argv[]){
	//Handle arguments
	string filename = "testinput.txt";
	if(argc >= 2){
		filename = argv[1];
	}

	//Initialize stuff
	FileInterface fInt;
	fInt.set_file(filename);
	Parser parser(&fInt);
	fInt.read_file();

	//Welcome Message
	cout << "Welcome to TextBuddy. " << filename.c_str() << " is ready for use.\n";


	//Get input and send to parser
	char* input = new char[1000];
	while(true){
		cout << "Command: ";
		cin.getline(input,1000,'\n');
		string output = parser.parse(input);
		
		//Break on exit command.
		if(strcmp(output.c_str(),"exit") == 0 ){
			break;
		}

		//Print output
		cout << output.c_str() << "\n";
	}


	//system("pause");
	return 0;
}